package mylist;

import java.util.Arrays;

import static java.lang.System.exit;

public class MyArrayList<I> implements IList{
    private Object[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 10;
    public MyArrayList() {
        this.elem = new Object[DEFAULT_SIZE];
        this.usedSize = 0;
    }

    private void checkCapacity()
    {
        if(isFull())
        {
            elem = Arrays.copyOf(elem, elem.length*2);
        }
    }

    @Override
    public void add(Object data) {
        checkCapacity();
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    private void addPosDataException(int pos) throws MyListException {
        if(pos < 0 || pos > this.usedSize)
        {
            throw new MyListException("插入元素下标异常: " + pos);
        }
    }
    @Override
    public void add(int pos, Object data) {
        try{
            addPosDataException(pos);
        }
        catch (MyListException e)
        {
            e.printStackTrace();
            exit(0);
        }
        checkCapacity();

        for(int i = this.usedSize - 1; i >= pos; i--)
        {
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    @Override
    public boolean contains(Object toFind) {
        if(isEmpty())
        {
            return false;
        }

        for(Object e : elem)
        {
            if(e.equals(toFind))
            {
                return true;
            }
        }

        return false;
    }

    @Override
    public int indexOf(Object toFind) {
        if(isEmpty())
        {
            return -1;
        }

        for(int i = 0; i < this.usedSize; i++)
        {
            if(elem[i].equals(toFind))
            {
                return i;
            }
        }

        return -1;
    }

    private void getPosException(int pos) throws MyListException {
        if(pos < 0 || pos >= this.usedSize)
        {
            throw new MyListException("获取指定下标的元素异常: " + pos);
        }

        if(isEmpty())
        {
            throw new MyListException("顺序表为空");
        }
    }
    @Override
    public Object get(int pos) {
        getPosException(pos);

        return elem[pos];
    }

    private void setPosValueException(int pos) throws MyListException
    {
        if(pos < 0 || pos >= this.usedSize)
        {
            throw new MyListException("修改元素下标异常: " + pos);
        }
    }
    @Override
    public void set(int pos, Object value) {
        setPosValueException(pos);

        elem[pos] = value;
    }

    @Override
    public void remove(Object toRemove) {
        int index = indexOf(toRemove);

        if(index == -1) return;

        for(int i = index; i < this.usedSize - 1; i++)
        {
            elem[i] = elem[i + 1];
        }

        this.usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        this.usedSize = 0;
    }

    @Override
    public void display() {
        for(int i = 0; i < this.usedSize; i++)
        {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return this.usedSize == elem.length;
    }

    @Override
    public boolean isEmpty() {
        return this.usedSize == 0;
    }
}
